import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'tag_selector.dart';

class CustomTagSelector<T> extends StatelessWidget {
  final List<T> options;
  final List<T> value;
  final ValueChanged<List<T>> onChanged;
  final bool multiChoice;
  final bool wrapped;
  final double spacing;
  final double runSpacing;
  final String Function(T)? labelBuilder;
  final Color? selectedBgColor;
  final Color? normalBgColor;
  final TextStyle? selectedTextStyle;
  final TextStyle? textStyle;

  const CustomTagSelector({
    super.key,
    required this.options,
    required this.value,
    required this.onChanged,
    this.multiChoice = true,
    this.wrapped = true,
    this.spacing = 6,
    this.runSpacing = 8,
    this.labelBuilder,
    this.selectedBgColor,
    this.normalBgColor,
    this.selectedTextStyle,
    this.textStyle,
  });

  @override
  Widget build(BuildContext context) {
    selectedBgColor ?? Theme.of(context).colorScheme.primary;
    normalBgColor ?? Colors.white;
    selectedTextStyle ??
        Theme.of(context).textTheme.labelMedium!.copyWith(color: Colors.white);
    textStyle ??
        Theme.of(context).textTheme.bodyMedium!.copyWith(color: Colors.black87);

    return TagSelector<T>(
      options: options,
      value: value,
      onChanged: onChanged,
      wrapped: wrapped,
      spacing: spacing.w,
      runSpacing: runSpacing.h,
      selectedBuilder: (context, val, selected) {
        final label = labelBuilder?.call(val) ?? val.toString();
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
          decoration: BoxDecoration(
            color: selectedBgColor!,
            borderRadius: BorderRadius.circular(12.w),
            border: Border.all(color: selectedBgColor!, width: 1),
          ),
          child: Text(label, style: selectedTextStyle),
        );
      },
      unselectedBuilder: (context, val, selected) {
        final label = labelBuilder?.call(val) ?? val.toString();
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
          decoration: BoxDecoration(
            color: normalBgColor,
            borderRadius: BorderRadius.circular(12.w),
            border: Border.all(color: Colors.white, width: 1),
          ),
          child: Text(label, style: textStyle),
        );
      },
    );
  }
}
